/**
 * hrm.
 */
function TasksList( parent, displayValue, isSubGroup )
{
    this.doInit.apply(this, arguments);
    this.tasks = new Array();
    this.displayValue = displayValue;
    this.parent = parent;
    this.isActionsSubGroup = isSubGroup;
    this.upDownEnabled = isSubGroup ? 1 : 0;
    this.deletable = isSubGroup ? 1 : 0;
    

}

TasksList.prototype = domplate(TestFireObject,
{
    selectable: 0,

/**
 *
 */
    init: function()
    {
    },

    isUpAvailable : function()
    {
        return this._isUpAvailable(this.parent.tasks);

    },

    isDownAvailable : function()
    {
        return this._isDownAvailable(this.parent.tasks);
    },

    moveDown : function()
    {
        return this._moveDown(this.parent.tasks);
    },

    moveUp : function()
    {
        return this._moveUp(this.parent.tasks);
    },
/**
 * Only display an expansion-available indicator if there is an action in this ActionList
 */
    getHasChildren: function()
    {
        return this.tasks.length != 0
    },

/**
 *
 */
    getChildrenToDisplay: function()
    {
        var ary = new Array();
        for ( var i = 0; i < this.tasks.length; ++i )
        {
            ary.push({name: this.tasks[i].displayValue, object: this.tasks[i] });
        }
        return ary;
    },





    doAddAssertion: function( targetObj, htmlFinder, finderName, assertion )
    {
        var frameActionables = FinderBuilder.getFrameActions(targetObj, this);
        var L = frameActionables.length;
        if(L > 0)
            assertion.args.isFrame = true;
        var valid = assertion.populate(this, targetObj, htmlFinder, finderName);

        if(!valid)
        {
            alert('Assertion is not valid (not yet implemented?): ' + finderName);
            return;
        }
        if( L > 0 )
        {
            var group = new TasksList(this, "Tasks Group", 1);
            group.deletable = true;
            for ( var i = 0; i < L; i++ )
            {
                group.tasks.push(frameActionables[i]);
            }
            group.tasks.push(assertion);
            this.tasks.push(group);
        }
        else {
            this.tasks.push(assertion);
        }
        Firebug.TestFire.refreshAll();
    },

    doAddActionable: function( targetObj, htmlFinder, finderName, actionable )
    {
        var frameActionables = FinderBuilder.getFrameActions(targetObj, this);
        var L = frameActionables.length;
        if(L > 0)
        {
            actionable.args.isFrame = true;
        }
        var valid = actionable.populate(this, targetObj, htmlFinder, finderName);
         if(!valid)
        {
            alert('Action is not valid (not yet implemented?): ' + finderName);
            return;
        }
        if( L > 0 )
        {
            var group = new TasksList(this, "Tasks Group", 1);
            group.deletable = true; 
            for ( var i = 0; i < L; i++ )
            {
                group.tasks.push(frameActionables[i]);
            }
            group.tasks.push(actionable);
            this.tasks.push(group);
        }
        else
        {
            this.tasks.push(actionable);
        }
        Firebug.TestFire.refreshAll();
    },

    open: function(element)
    {
        var childEls = element.childNodes
        var obj;
        for(var i = 0, L = childEls.length; i < L; i++){
            if(childEls[i].nodeType == element.ELEMENT_NODE)
            {
                var actionEl = childEls.item(i);
                var name = actionEl.localName;
                var el;
                if(name == 'ElementAction')
                {
                    el = this.getFirstChildElement(actionEl);
                    el = this.getLastChildElement(el);
                    obj = new Actionable(this);
                    obj.open(actionEl, el);
                    obj.init(this, el.localName  );
                }
                else if(name == 'Assertion')
                {
                    el = this.getFirstChildElement(actionEl);
                    obj = eval("new " + el.localName + "();");
                    obj.open(actionEl);
                    obj.parent = this;
                }
                else if(name == 'BrowserCommand')
                {
                    el = this.getLastChildElement(actionEl);
                    obj = new Actionable(this);
                    obj.open(actionEl);
                    obj.init(this, el.localName  );
                }
                else if(name == 'taskGroup')
                {
                    obj = new TasksList(this, 'Task Group', 1);
                    obj.open(actionEl);
                    obj.init();
                }
                else
                {
                    alert("Handle: " + name);
                }

                this.tasks.push(obj);

            }

        }
    },

    
    toXMLStringAry: function()
    {
        var ret = new Array();
        try
        {
            if(this.isActionsSubGroup)
            {
                ret = ret.concat("<tf:taskGroup>");
            }
            for ( var i = 0,n = this.tasks.length; i < n; ++i )
            {
                ret = ret.concat(this.tasks[i].toXMLStringAry());
            }
            if(this.isActionsSubGroup)
            {
                ret = ret.concat("</tf:taskGroup>");
            }
        }
        catch( ex )
        {
            Firebug.TestFire.util.tf_debug(ex);
        }
        return ret;
    },

    toString : function()
    {
        if(this.isActionsSubGroup)
        {
            return this.tasks[this.tasks.length - 1].toString();
        }
        return 'Tasks ( ' + this.tasks.length + ' ) '
    }



});